package cn.iocoder.yudao.module.forward.controller.admin.license;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.license.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.license.LicenseDO;
import cn.iocoder.yudao.module.forward.service.license.LicenseService;

@Tag(name = "管理后台 - 授权码")
@RestController
@RequestMapping("/forward/license")
@Validated
public class LicenseController {

    @Resource
    private LicenseService licenseService;

    @PostMapping("/create")
    @Operation(summary = "创建授权码")
    @PreAuthorize("@ss.hasPermission('forward:license:create')")
    public CommonResult<Integer> createLicense(@Valid @RequestBody LicenseSaveReqVO createReqVO) {
        return success(licenseService.createLicense(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新授权码")
    @PreAuthorize("@ss.hasPermission('forward:license:update')")
    public CommonResult<Boolean> updateLicense(@Valid @RequestBody LicenseSaveReqVO updateReqVO) {
        licenseService.updateLicense(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除授权码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:license:delete')")
    public CommonResult<Boolean> deleteLicense(@RequestParam("id") Integer id) {
        licenseService.deleteLicense(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得授权码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:license:query')")
    public CommonResult<LicenseEditRespVO> getLicense(@RequestParam("id") Integer id) {
        LicenseDO license = licenseService.getLicense(id);
        return success(BeanUtils.toBean(license, LicenseEditRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得授权码分页")
    @PreAuthorize("@ss.hasPermission('forward:license:query')")
    public CommonResult<PageResult<LicenseRespVO>> getLicensePage(@Valid LicensePageReqVO pageReqVO) {
        PageResult<LicenseDO> pageResult = licenseService.getLicensePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LicenseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出授权码 Excel")
    @PreAuthorize("@ss.hasPermission('forward:license:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLicenseExcel(@Valid LicensePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LicenseDO> list = licenseService.getLicensePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "授权码.xls", "数据", LicenseRespVO.class,
                        BeanUtils.toBean(list, LicenseRespVO.class));
    }

}